package cn.sirun.com.fca.map.view.impl;

import android.app.Activity;
import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tencent.cloud.iov.flow.view.IContentViewContainer;
import com.tencent.cloud.iov.kernel.model.BaseResponse;
import com.tencent.cloud.iov.router.Router;
import com.tencent.cloud.iov.util.ActivityUtils;
import com.tencent.cloud.iov.util.ResourcesUtils;
import com.tencent.cloud.iov.util.storage.TXSharedPreferencesUtils;
import com.tencent.cloud.uikit.utils.SystemUiUtils;

import java.util.ArrayList;
import java.util.List;

import cn.sirun.com.fca.map.R;
import cn.sirun.com.fca.map.adapter.InventoryAdapter;
import cn.sirun.com.fca.map.constant.MapActivityConst;
import cn.sirun.com.fca.map.model.bean.AddressBean;
import cn.sirun.com.fca.map.model.repose.MapCarLocation;
import cn.sirun.com.fca.map.model.repose.MapCollectedResponse;
import cn.sirun.com.fca.map.model.repose.MapHomeCompanyResponse;
import cn.sirun.com.fca.map.presenter.IMapPresenter;
import cn.sirun.com.fca.map.presenter.impl.MapPresenter;
import cn.sirun.com.fca.map.view.IMapCollectedView;
import cn.sirun.com.fca.map.widget.SlideRecyclerView;


public class MapCollectedFlowView extends MapBaseLoadingFlowView<IMapPresenter> implements
        IMapCollectedView, View.OnClickListener {

    private SlideRecyclerView recycler_view_list;
    private InventoryAdapter inventoryAdapter;
    private List<AddressBean> addressListBean = new ArrayList<>();
    private LinearLayout ll_homeAddress;
    private LinearLayout ll_companyAddress;
    private AddressBean homeAddress;
    private AddressBean companyAddress;
    private TextView tv_home;
    private TextView tv_home_address;
    private TextView tv_company;
    private TextView tv_company_address;
    private FrameLayout fl_nodata;
    private String din;
    private int page = 1;
    private SwipeRefreshLayout swif;
    private LinearLayoutManager linearLayoutManager;
    private int totalPage;

    public MapCollectedFlowView(Activity activity) {
        super(activity);
    }

    public MapCollectedFlowView(Activity activity, IContentViewContainer container) {
        super(activity, container);

    }

    @Override
    public void onCreate() {
        super.onCreate();
        setUpActionBar();
        initView();
        din = TXSharedPreferencesUtils.getValue("car_activate_info");
//        if (!TextUtils.isEmpty(din)) {
//            getPresenter().collectedAddress(din, page + "", "10");
//            getPresenter().getHomeAddress(din);
//        }
    }

    private void setUpActionBar() {
        final Activity activity = getActivity();
        SystemUiUtils.showActionBar(
                activity, ResourcesUtils.getString(R.string.map_title_collect),
                ResourcesUtils.getDrawable(R.color.white));
    }

    private void initView() {
        setContentView(R.layout.map_my_collected_fragment);
        final Activity activity = getActivity();
//        swif = activity.findViewById(R.id.swif);
        recycler_view_list = activity.findViewById(R.id.lv_map_collected);
        recycler_view_list.setLayoutManager(new LinearLayoutManager(getContext(),
                LinearLayoutManager.VERTICAL, false));
        DividerItemDecoration itemDecoration = new DividerItemDecoration(getContext(),
                DividerItemDecoration.VERTICAL);
        itemDecoration.setDrawable(ContextCompat.getDrawable(getContext(), R.drawable
                .divider_inset));
        recycler_view_list.addItemDecoration(itemDecoration);
        ll_homeAddress = activity.findViewById(R.id.ll_collected_home);
        ll_homeAddress.setOnClickListener(this);
        ll_companyAddress = activity.findViewById(R.id.ll_collected_company);
        ll_companyAddress.setOnClickListener(this);
        tv_home = activity.findViewById(R.id.tv_map_collected_home);
        tv_home_address = activity.findViewById(R.id.tv_map_collected_homeaddress);
        tv_company = activity.findViewById(R.id.tv_map_collected_company);
        tv_company_address = activity.findViewById(R.id.tv_map_collected_company_address);
        fl_nodata = activity.findViewById(R.id.fl_collected_nodata);

    }

    @Override
    protected IMapPresenter createPresenter() {
        return new MapPresenter();
    }

    @Override
    public void getMapCollected(final BaseResponse<MapCollectedResponse> response) {
        int code = response.getCode();
        if (code == 0) {
            totalPage=response.getData().getTotal()/10;
            addressListBean.addAll(response.getData().getAddressList());
            inventoryAdapter = new InventoryAdapter(getContext(), addressListBean);
            recycler_view_list.setAdapter(inventoryAdapter);
            recycler_view_list.setVisibility(View.VISIBLE);
            recycler_view_list.addOnScrollListener(new RecyclerView.OnScrollListener() {
                @Override
                public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                    super.onScrollStateChanged(recyclerView, newState);
                    if (newState == RecyclerView.SCROLL_STATE_IDLE&&page<=totalPage) {
                        page++;
                        if (!TextUtils.isEmpty(din)) {
                            getPresenter().collectedAddress(din, page + "", "10");
                        }else {
                            getPresenter().collectedAddress("", page + "", "10");
                        }
                        inventoryAdapter.notifyDataSetChanged();
                    }
                }
            });
            fl_nodata.setVisibility(View.GONE);
            //点击查看
            inventoryAdapter.setItemClickLister(new InventoryAdapter.OnItemClickLister() {
                @Override
                public void onItemClickLister(View view, int position) {
                    Intent intent = Router.fromPath(MapActivityConst.Path.MAP_SEND_CAR);
                    intent.putExtra("set", "send");
                    intent.putExtra("addresslat", addressListBean.get(position).getLatitude() + "");
                    intent.putExtra("addresslng", addressListBean.get(position).getLongitude() +
                            "");
                    intent.putExtra("title", addressListBean.get(position).getName());
                    intent.putExtra("address", addressListBean.get(position).getAddress());
                    intent.putExtra("favoritesId",addressListBean.get(position).getFavoriteAddressId());
                    ActivityUtils.startActivity(getActivity(), intent);
                }
            });
            //删除操作
            inventoryAdapter.setOnDeleteClickListener(new InventoryAdapter.OnDeleteClickLister() {
                @Override
                public void onDeleteClick(View view, int position) {
                    if (!TextUtils.isEmpty(din)) {
                        getPresenter().deleteCollectAddress(din, addressListBean.get(position)
                                .getFavoriteAddressId());
                    }else {
                        getPresenter().deleteCollectAddress("", addressListBean.get(position)
                                .getFavoriteAddressId());
                    }
                    addressListBean.remove(position);
                    inventoryAdapter.notifyDataSetChanged();
                    recycler_view_list.closeMenu();
                }
            });
        }
    }

    @Override
    public void addMapCollected(BaseResponse<BaseResponse> response) {

    }

    @Override
    public void deleteMapCollected(BaseResponse<BaseResponse> response) {

    }

    @Override
    public void mapHomeCompany(BaseResponse<MapHomeCompanyResponse> response) {
        int code = response.getCode();
        if (code == 0) {
            if (!TextUtils.isEmpty(response.getData().getHomeAddress().getName())) {
                tv_home.setText(response.getData().getHomeAddress().getName());
                ll_homeAddress.setVisibility(View.VISIBLE);
                fl_nodata.setVisibility(View.GONE);
            }
            if (!TextUtils.isEmpty(response.getData().getHomeAddress().getAddress())) {
                homeAddress = new AddressBean();
                homeAddress.setLatitude(response.getData().getHomeAddress().getLatitude());
                homeAddress.setLongitude(response.getData().getHomeAddress().getLongitude());
                homeAddress.setName(response.getData().getHomeAddress().getAddress());
                homeAddress.setAddress(response.getData().getHomeAddress().getAddress());
                tv_home_address.setText(response.getData().getHomeAddress().getAddress());
                ll_homeAddress.setVisibility(View.VISIBLE);
                fl_nodata.setVisibility(View.GONE);
            }
            if (!TextUtils.isEmpty(response.getData().getCompanyAddrss().getName())) {
                companyAddress = new AddressBean();
                companyAddress.setLatitude(response.getData().getCompanyAddrss().getLatitude());
                companyAddress.setLongitude(response.getData().getCompanyAddrss().getLongitude());
                companyAddress.setName(response.getData().getCompanyAddrss().getAddress());
                companyAddress.setAddress(response.getData().getCompanyAddrss().getAddress());
                tv_company.setText(response.getData().getCompanyAddrss().getName());
                ll_companyAddress.setVisibility(View.VISIBLE);
                fl_nodata.setVisibility(View.GONE);
            }
            if (!TextUtils.isEmpty(response.getData().getCompanyAddrss().getAddress())) {
                tv_company_address.setText(response.getData().getCompanyAddrss().getAddress());
                ll_companyAddress.setVisibility(View.VISIBLE);
                fl_nodata.setVisibility(View.GONE);
            }
        }
    }

    @Override
    public void mapSendToCar(BaseResponse<MapHomeCompanyResponse> response) {

    }

    @Override
    public void getLastLocation(BaseResponse<MapCarLocation> response) {

    }

    @Override
    public void updateLocation(BaseResponse<MapCarLocation> response) {

    }


    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.ll_collected_home) {
            Intent intent = Router.fromPath(MapActivityConst.Path.MAP_SEND_CAR);
            intent.putExtra("set", "modifyhome");
            intent.putExtra("addresslat", homeAddress.getLatitude() + "");
            intent.putExtra("addresslng", homeAddress.getLongitude() + "");
            intent.putExtra("title", homeAddress.getName());
            intent.putExtra("address", homeAddress.getAddress());
            ActivityUtils.startActivity(getActivity(), intent);
        } else if (v.getId() == R.id.ll_collected_company) {
            Intent intent = Router.fromPath(MapActivityConst.Path.MAP_SEND_CAR);
            intent.putExtra("set", "modifycompany");
            intent.putExtra("addresslat", companyAddress.getLatitude() + "");
            intent.putExtra("addresslng", companyAddress.getLongitude() + "");
            intent.putExtra("title", companyAddress.getName());
            intent.putExtra("address", companyAddress.getAddress());
            ActivityUtils.startActivity(getActivity(), intent);
        }
    }

    @Override
    public void onStart() {
        super.onStart();
        din = TXSharedPreferencesUtils.getValue("car_activate_info");
        if (!TextUtils.isEmpty(din)) {
            getPresenter().collectedAddress(din, page+"", "10");
            getPresenter().getHomeAddress(din);
        }else {
            getPresenter().collectedAddress("", page+"", "10");
            getPresenter().getHomeAddress("");
        }

    }
}
